home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / SimpleAttributeSet.java < prev    next >
Text File  |  1998-06-30  |  9KB  |  311 lines

  1. /*
  2.  * @(#)SimpleAttributeSet.java    1.23 98/04/09
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing.text;
  21.  
  22. import java.util.Hashtable;
  23. import java.util.Enumeration;
  24. import java.io.Serializable;
  25.  
  26. /**
  27.  * A straightforward implementation of MutableAttributeSet using a 
  28.  * hash table.
  29.  * <p>
  30.  * Warning: serialized objects of this class will not be compatible with
  31.  * future swing releases.  The current serialization support is appropriate 
  32.  * for short term storage or RMI between Swing1.0 applications.  It will
  33.  * not be possible to load serialized Swing1.0 objects with future releases
  34.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  35.  * baseline for the serialized form of Swing objects.
  36.  *
  37.  * @version 1.23 04/09/98
  38.  * @author unknown
  39.  */
  40. public class SimpleAttributeSet implements MutableAttributeSet, Serializable
  41. {
  42.     /**
  43.      * An empty attribute set.
  44.      */
  45.     public static final AttributeSet EMPTY = new SimpleAttributeSet();
  46.  
  47.     private Hashtable table = new Hashtable(3);
  48.  
  49.     /**
  50.      * Creates a new attribute set.
  51.      */
  52.     public SimpleAttributeSet() {
  53.     }
  54.  
  55.     /**
  56.      * Creates a new attribute set based on a supplied set of attributes.
  57.      *
  58.      * @param source the set of attributes
  59.      */
  60.     public SimpleAttributeSet(AttributeSet source) {
  61.         addAttributes(source);
  62.     }
  63.  
  64.     private SimpleAttributeSet(Hashtable table) {
  65.         this.table = table;
  66.     }
  67.  
  68.     /**
  69.      * Checks whether the set of attributes is empty.
  70.      *
  71.      * @return true if the set is empty else false
  72.      */
  73.     public boolean isEmpty()
  74.     {
  75.         return table.isEmpty();
  76.     }
  77.  
  78.     /**
  79.      * Gets a count of the number of attributes.
  80.      *
  81.      * @return the count
  82.      */
  83.     public int getAttributeCount() {
  84.         return table.size();
  85.     }
  86.  
  87.     /**
  88.      * Tells whether a given attribute is defined.
  89.      *
  90.      * @param attrName the attribute name
  91.      * @return true if the attribute is defined
  92.      */
  93.     public boolean isDefined(Object attrName) {
  94.     return table.containsKey(attrName);
  95.     }
  96.  
  97.     /**
  98.      * Compares two attribute sets.
  99.      *
  100.      * @param attr the second attribute set
  101.      * @return true if equathe listl
  102.      */
  103.     public boolean isEqual(AttributeSet attr) {
  104.     return ((getAttributeCount() == attr.getAttributeCount()) &&
  105.         containsAttributes(attr));
  106.     }
  107.  
  108.     /**
  109.      * Makes a copy of the attributes.
  110.      *
  111.      * @return the copy
  112.      */
  113.     public AttributeSet copyAttributes() {
  114.     return (AttributeSet) clone();
  115.     }
  116.  
  117.     /**
  118.      * Gets the names of the attributes in the set.
  119.      *
  120.      * @return the names as an Enumeration
  121.      */
  122.     public Enumeration getAttributeNames() {
  123.         return table.keys();
  124.     }
  125.  
  126.     /**
  127.      * Gets the value of an attribute.
  128.      *
  129.      * @param name the attribute name
  130.      * @return the value
  131.      */
  132.     public Object getAttribute(Object name) {
  133.         Object value = table.get(name);
  134.     if (value == null) {
  135.         AttributeSet parent = getResolveParent();
  136.         if (parent != null) {
  137.         value = parent.getAttribute(name);
  138.         }
  139.     }
  140.     return value;
  141.     }
  142.  
  143.     /**
  144.      * Checks whether the attribute list contains a
  145.      * specified attribute name/value pair.
  146.      *
  147.      * @param name the name
  148.      * @param value the value
  149.      * @return true if the name/value pair is in the list
  150.      */
  151.     public boolean containsAttribute(Object name, Object value) {
  152.         return value.equals(getAttribute(name));
  153.     }
  154.  
  155.     /**
  156.      * Checks whether the attribute list contains all the
  157.      * specified name/value pairs.
  158.      *
  159.      * @param attributes the attribute list
  160.      * @return true if the list contains all the name/value pairs
  161.      */
  162.     public boolean containsAttributes(AttributeSet attributes) {
  163.         boolean result = true;
  164.  
  165.         Enumeration names = attributes.getAttributeNames();
  166.         while (result && names.hasMoreElements()) {
  167.             Object name = names.nextElement();
  168.             result = attributes.getAttribute(name).equals(getAttribute(name));
  169.         }
  170.  
  171.         return result;
  172.     }
  173.  
  174.     /**
  175.      * Adds an attribute to the list.
  176.      *
  177.      * @param name the attribute name
  178.      * @param value the attribute value
  179.      */
  180.     public void addAttribute(Object name, Object value) {
  181.         table.put(name, value);
  182.     }
  183.  
  184.     /**
  185.      * Adds a set of attributes to the list.
  186.      *
  187.      * @param attributes the set of attributes to add
  188.      */
  189.     public void addAttributes(AttributeSet attributes) {
  190.         Enumeration names = attributes.getAttributeNames();
  191.         while (names.hasMoreElements()) {
  192.             Object name = names.nextElement();
  193.             addAttribute(name, attributes.getAttribute(name));
  194.         }
  195.     }
  196.  
  197.     /**
  198.      * Removes an attribute from the list.
  199.      *
  200.      * @param name the attribute name
  201.      */
  202.     public void removeAttribute(Object name) {
  203.         table.remove(name);
  204.     }
  205.  
  206.     /**
  207.      * Removes a set of attributes from the list.
  208.      *
  209.      * @param names the set of names to remove
  210.      */
  211.     public void removeAttributes(Enumeration names) {
  212.         while (names.hasMoreElements())
  213.             removeAttribute(names.nextElement());
  214.     }
  215.  
  216.     /**
  217.      * Removes a set of attributes from the list.
  218.      *
  219.      * @param attributes the set of attributes to remove
  220.      */
  221.     public void removeAttributes(AttributeSet attributes) {
  222.         Enumeration names = attributes.getAttributeNames();
  223.         while (names.hasMoreElements()) {
  224.             Object name = names.nextElement();
  225.             Object value = attributes.getAttribute(name);
  226.             if (value.equals(getAttribute(name)))
  227.                 removeAttribute(name);
  228.         }
  229.     }
  230.  
  231.     /**
  232.      * Gets the resolving parent.  This is the set
  233.      * of attributes to resolve through if an attribute
  234.      * isn't defined locally.  This is null if there 
  235.      * are no other sets of attributes to resolve 
  236.      * through.
  237.      *
  238.      * @return the parent
  239.      */
  240.     public AttributeSet getResolveParent() {
  241.     return (AttributeSet) table.get(StyleConstants.ResolveAttribute);
  242.     }
  243.  
  244.     /**
  245.      * Sets the resolving parent.
  246.      *
  247.      * @param parent the parent
  248.      */
  249.     public void setResolveParent(AttributeSet parent) {
  250.     addAttribute(StyleConstants.ResolveAttribute, parent);
  251.     }
  252.  
  253.     // --- Object methods ---------------------------------
  254.  
  255.     /**
  256.      * Clones a set of attributes.
  257.      *
  258.      * @return the new set of attributes
  259.      */
  260.     public Object clone() {
  261.         return new SimpleAttributeSet((Hashtable)table.clone());
  262.     }
  263.  
  264.     /**
  265.      * Returns a hashcode for this set of attributes.
  266.      * @return     a hashcode value for this set of attributes.
  267.      */
  268.     public int hashCode() {
  269.     return table.hashCode();
  270.     }
  271.  
  272.     /**
  273.      * Compares this object to the specifed object.
  274.      * The result is <code>true</code> if and only if the argument is not 
  275.      * <code>null</code> and is a <code>Font</code> object with the same 
  276.      * name, style, and point size as this font. 
  277.      * @param     obj   the object to compare this font with.
  278.      * @return    <code>true</code> if the objects are equal; 
  279.      *            <code>false</code> otherwise.
  280.      */
  281.     public boolean equals(Object obj) {
  282.     if (obj instanceof AttributeSet) {
  283.         AttributeSet attrs = (AttributeSet) obj;
  284.         return isEqual(attrs);
  285.     }
  286.     return false;
  287.     }
  288.  
  289.     /**
  290.      * Converts the attribute set to a String.
  291.      *
  292.      * @return the string
  293.      */
  294.     public String toString() {
  295.     String s = "";
  296.         Enumeration names = getAttributeNames();
  297.         while (names.hasMoreElements()) {
  298.             Object key = names.nextElement();
  299.             Object value = getAttribute(key);
  300.         if (value instanceof AttributeSet) {
  301.         // don't go recursive
  302.         s = s + key + "=**AttributeSet** ";
  303.         } else {
  304.         s = s + key + "=" + value + " ";
  305.         }
  306.     }
  307.     return s;
  308.     }
  309. }
  310.  
  311.